import { Tree, ResultData } from "@/api/interface/index";
import { TreePort } from "@/api/config/servicePort";
import http from "@/api";

/**
 * @name 用户管理模块
 */

// 获取菜单全树
export const getMenuTreeAll = () => {
  return http.post<ResultData<Tree.menuObject[]>>(TreePort + `/menu/menuTreeAll`, {});
};
// 用户菜单树
export const getUserTreeMenu = () => {
  return http.post<ResultData<Tree.menuObject[]>>(TreePort + `/menu/menuTreeUser`, {});
};

// 获取用户菜单ids
export const getMenuIds = (user_id: number) => {
  return http.post<ResultData<number[]>>(TreePort + `/menu/getUserMenuIds`, { user_id });
};

// 新增菜单
export const addMenu = (params: Tree.menuObject) => {
  return http.post(TreePort + `/menu/add`, { ...params, url: params.path, pid: (params.pid as any).slice(-1)[0] });
};

// 更新用户菜单
export const updateUserMenu = (user_id, menu_ids) => {
  return http.post(TreePort + `/menu/updateUserMenu`, { user_id, menu_ids });
};

// 编辑菜单
export const editMenu = (params: Tree.menuObject) => {
  console.log(params, "params");
  return http.post(TreePort + `/menu/update`, {
    url: params.path,
    memo: params.memo,
    pid: Array.isArray(params.pid) ? params.pid.slice(-1)[0] : params.pid,
    id: params.id,
    status: params.status,
    type: params.type,
    // code: params.code,
    name: params.name,
    identification: params.identification,
    icon: params.icon,
    sort: params.sort
    // creator_id: params.creator_id,
    // updater_id: params.updater_id,
    // created_at: params.created_at,
    // updated_at: params.updated_at
    // children: params.children
  });
};

// 删除菜单
export const deleteMenu = (params: { id: string[] }) => {
  return http.post(TreePort + `/menu/delete`, params);
};
